package com.vgo.crop.view;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import xf.tools.Other;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;

/**
 * 
 * <p><b>Title：</b> 裁剪界面</p>
 * <p><b>Description：</b> </p>
 * @author tangxin
 * @date 2016年12月6日 下午3:51:23
 */
public class Crop_Activity extends BaseActivity implements OnClickListener {

	private ClipImageLayout mClipImageLayout;
	Button ok;
	Button back;
	Button ok2;
	Button back2;

	Button sure_btn, cancel_btn;

	ImageView back_btn;
	
	@Override
	protected void onResume() {
		BaseActivity.activity = this; // TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.crop_mainactivity);
		BaseActivity.activity = this;

		mClipImageLayout = (ClipImageLayout) findViewById(R.id.id_clipImageLayout);
		ok = (Button) findViewById(R.id.ok);
		back = (Button) findViewById(R.id.back);
		ok2 = (Button) findViewById(R.id.ok2);
		back2 = (Button) findViewById(R.id.back2);
		sure_btn = (Button) findViewById(R.id.sure_btn);
		cancel_btn = (Button) findViewById(R.id.cancel_btn);
		back_btn=(ImageView) findViewById(R.id.back_btn);
		SetRelativiLayoutPadding(R.id.main_head_top_rela);
		back_btn.setVisibility(View.GONE);
		sure_btn.setVisibility(View.VISIBLE);
		cancel_btn.setVisibility(View.VISIBLE);
		cancel_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		sure_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					EmmApplication.bitmap = mClipImageLayout.clip();
					sendBroadcast(new Intent(Other.VGOPLAY_1));
					finish();
				} catch (Exception e) {
				}
			}
		});
		ok.setOnClickListener(this);
		back.setOnClickListener(this);
		ok2.setOnClickListener(this);
		back2.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ok:
			try {
				Bitmap bitmap = mClipImageLayout.clip();
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
				byte[] datas = baos.toByteArray();
				try {
					baos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				byte[] encode = Base64.encode(datas, Base64.DEFAULT);
				String photo2 = new String(encode);
				EmmApplication.setData("uri_byte", photo2);

				sendBroadcast(new Intent(Other.VGOPLAY_1));
				finish();
			} catch (Exception e) {
			}

			break;
		case R.id.back:
			finish();
			break;

		case R.id.back2:
			finish();
			break;

		case R.id.ok2:
			try {
				EmmApplication.bitmap = mClipImageLayout.clip();
				// Bitmap bitmap = mClipImageLayout.clip();
				// ByteArrayOutputStream baos = new ByteArrayOutputStream();
				// bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
				// byte[] datas = baos.toByteArray();
				// try {
				// baos.close();
				// } catch (IOException e) {
				// e.printStackTrace();
				// }
				// byte[] encode = Base64.encode(datas, Base64.DEFAULT);
				// String photo2 = new String(encode);
				// EmmApplication.setData("uri_byte", photo2);// 获取之后直接上传

				sendBroadcast(new Intent(Other.VGOPLAY_1));
				finish();
			} catch (Exception e) {
				// TODO: handle exception
			}
			break;
		default:
			break;
		}
	}

}
